import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/detail/:id',
    name: 'detail',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/DetailView.vue'),
    meta:{
      hideTabbar:true
    }
  },
  {
    path: '/category',//类别
    name: 'category',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/CategoryView.vue')
  },
  {
    path: '/search',//搜索
    name: 'search',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/SearchView.vue'),
    meta:{
      hideTabbar:true
    }
  },
  {
    path: '/mine',//我的
    name: 'mine',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/MineView.vue'),
    meta:{
      auth:true//挡墙路由要权限
    }
  },
  {
    path: '/cart',//购物车
    name: 'cart',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/CartView.vue')
  },
  {
    path: '/login',//购物车
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/LoginView.vue'),
    meta:{
      hideTabbar:true
    }
  },
]

//解决 从a到a 以及冲顶向的做
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
};





const router = new VueRouter({
  routes
})


//全局路由守卫
//t跳转到f 当前n 下一步
import store from '@/store'
router.beforeEach((t,f,n)=>{
  if(t.meta.auth){
    // yao
    //如果登录去下一步为等去登录凭借/next=t.path
    if(store.getters.getUserInfo){
      n()
    }else{
      n({
        name:'login',
        query:{
          next:t.path//相当于/cart
        }
      })
    }
  }else{
    //不要权限直接去下一步
    n()
  }
})


export default router
